package main

import (
	"jinquan.supor.com/common"
	"jinquan.supor.com/samples/mongo/docs"
	"go.mongodb.org/mongo-driver/bson"
	"strings";"fmt"
)

func GetDoc(clnt *common.MongoClient, db string, collname string, 
			k1 string, v1 string, 
			k2 string, v2 string, 
			docStruct *testdoc.UserDoc) error {
	var err error
	coll := common.Mongo_ObjGetColl(clnt, db, collname)
	if err = coll.FindOne(clnt.T, bson.D{{strings.ToLower(k1), v1},{strings.ToLower(k2),v2}}).Decode(&docStruct); err != nil {
		common.CheckErr(err)
	}
	return err
}

func main() {
	//Pod 内部域名格式: $service.$namespace.svc.cluster.local
	/*
	clnt, _ := common.Mongo_ServerConn(`mongodb://mongo-0.mongo.mongo-ns.svc.cluster.local:27017,
											      mongo-1.mongo.mongo-ns.svc.cluster.local:27017,
												  mongo-2.mongo.mongo-ns.svc.cluster.local:27017/?slaveOk=true`, 
												  "secondary")
	*/
	clnt, _ := common.Mongo_ServerConn(`mongodb://mongodb.jinquan.svc.cluster.local:27017`, "primary")
	var docArr = testdoc.GenDocArray(1)
	k1 := "Email";v1 := common.IfVal_string(docArr[0], k1)
	k2 := "Username";v2 := common.IfVal_string(docArr[0], k2)
	//fmt.Printf("%s: %s\n", k1,v1);fmt.Printf("%s: %s\n", k2,v2)
	fmt.Printf("Original Data ->> %v\n", docArr[0])
	common.Mongo_CollAddDoc(clnt, "db_4", "coll_1", docArr[0])

	var docSt1 testdoc.UserDoc
	_ = GetDoc(clnt , "db_4", "coll_1", 
							k1,v1, 
							k2,v2, 
							&docSt1) 
	fmt.Printf("main.GetDoc   ->> %v\n", docSt1)

	/* 获取返回Document的某个字段数据 */
	var docStX testdoc.UserDoc;	var docSt2 = testdoc.AllocDoc()
	docSt2[0], _ = common.Mongo_GetDoc(clnt , "db_4", "coll_1", 
							k1,v1, 
							k2,v2, 
							&docStX)

	m := common.If2Map(docSt2[0])	//从MongoDB取出来的Key都是小写
	k := strings.ToLower("Email")
	fmt.Printf("%s: %s\n", k, m[k])
}